/**
 * Created by yangshengjiepro on 15/5/20.
 */

var UiHelper = function () {
    this.flag = 0;
};

var OKTOUCH = false;

//初始化开始UI
UiHelper.initStartUI = function(){
    if(mainLayer_Root!=null)
    {
        //var sp_logo=cc.Sprite(res.UI_logo);
        //mainLayer_Root.sp_logo=sp_logo;
        //sp_logo.attr({
        //    x:basesize.width/2,
        //    y:basesize.height/2+300,
        //    scale:1,
        //    ratation:0
        //});
        //sp_logo.setAnchorPoint(cc.p(0.5,0.5));
        //mainLayer_Root.addChild(sp_logo);

        Mlog.c("NO1111");

        ////创建开始按钮
        var bt_play = cc.Sprite.create(res.UI_logo);
        mainLayer_Root.bt_play=bt_play;
        bt_play.attr({
            x : basesize.width/2,
            y : basesize.height/2+100,
            scale:1,
            ratation:0
        });
        bt_play.setTag(4);
        bt_play.setAnchorPoint(cc.p(0.5,0.5));
        mainLayer_Root.addChild(bt_play);


        ////创建开始按钮
        var bt_start = cc.Sprite.create(res.UI_bt_start);
        mainLayer_Root.bt_start=bt_start;
        bt_start.x = mainLayer_Root.getContentSize().width/2;
        bt_start.y = mainLayer_Root.getContentSize().height/2-80;
        bt_start.setTag(11111);
        //bt_start._addEventListener(UiHelper.TouchEven_Start, bt_start);
        mainLayer_Root.addChild(bt_start,1);
        cc.eventManager.addListener(listener_Start, bt_start);


        OKTOUCH=true;
    }
};

var gamestate = "0";

var HEROUP_OR_DOWN = false;

//点击LOGO开始执行游戏
var listener_Start = cc.EventListener.create({
    event: cc.EventListener.TOUCH_ONE_BY_ONE,
    swallowTouches: true,
    onTouchBegan: function (touch, event) {

        return true;
    },
    onTouchEnded: function (touch, event) {
        var target = event.getCurrentTarget();
        var tagnum =  target.getTag();
        Mlog.c("listener_Start>>>",tagnum);

        if(OKTOUCH==true)
        {
            if(gamestate=="start")
            {
                //此时点击切换主角上下方向
                if(HEROUP_OR_DOWN==false)
                {
                    if(mainLayer_Root.hero!=null)
                    {
                        var cp = cc.p(110,mainLayer_Root.getContentSize().height/2-32);
                        mainLayer_Root.hero.setPosition(cp);
                        mainLayer_Root.hero.setScaleY(-1);
                    }
                    HEROUP_OR_DOWN=true;
                }
                else
                {
                    var cp = cc.p(110,mainLayer_Root.getContentSize().height/2+32);
                    mainLayer_Root.hero.setPosition(cp);
                    mainLayer_Root.hero.setScaleY(1);
                    HEROUP_OR_DOWN=false;
                }

            }

            if(gamestate=="0")
            {
                Mlog.c("cc22");
                gamestate="run";
                UiHelper.StartGame();
                GameOver.toClose();
            }


        }


        return true;
    }
});


var NowStoneNum = 1;

var NowStoneTag = 8888;

var SP_STONE_1 = null;
var SP_STONE_2 = null;
var SP_STONE_3 = null;


UiHelper.StartGame = function(){

    if(mainLayer_Root.bt_play!=null)
    {
        var act_move_up = cc.moveBy(0.66,cc.p(0,800));
        mainLayer_Root.bt_play.runAction(act_move_up);

        var act_move_down = cc.moveBy(0.66,cc.p(0,-1200));
        mainLayer_Root.bt_start.runAction(act_move_down);
    }
    var actdelay = cc.DelayTime.create(0.66);
    var todo_act = cc.Sequence.create(actdelay,cc.callFunc(UiHelper.ToStartGame, mainLayer_Root));
    mainLayer_Root.runAction(todo_act);

};

var Score_NUM = 0;

UiHelper.ToStartGame = function(){

    //出现一条线
    //创建开始按钮
    var goto = cc.Sprite.create(res.UI_goto);
    mainLayer_Root.goto=goto;
    goto.x = 0;
    goto.y = mainLayer_Root.getContentSize().height/2;
    mainLayer_Root.addChild(goto);

    var baseline = cc.Sprite.create(res.UI_baseline);
    mainLayer_Root.baseline=baseline;
    baseline.x = 0;
    baseline.y = mainLayer_Root.getContentSize().height/2;
    mainLayer_Root.addChild(baseline);

    var act_move_to1 = cc.moveBy(1,cc.p(640+50,0));
    var actdelay = cc.DelayTime.create(0.33)
    var todo_act = cc.Sequence.create(actdelay,act_move_to1);
    var act_move_to2 = cc.moveBy(0.33,cc.p(640/2,0));
    mainLayer_Root.goto.runAction(todo_act);
    mainLayer_Root.baseline.runAction(act_move_to2);

    //积分器出现
    var jifenbd = cc.Sprite.create(res.UI_fenshubd);
    mainLayer_Root.jifenbd=jifenbd;
    jifenbd.x = mainLayer_Root.getContentSize().width/2;
    jifenbd.y = mainLayer_Root.getContentSize().height+150;
    mainLayer_Root.addChild(jifenbd);

    var fenshu = cc.LabelTTF.create(Score_NUM+"","微软雅黑",40+6);
    fenshu.setColor(new cc.Color(255,0,0,0));
    mainLayer_Root.fenshu=fenshu;
    fenshu.setPosition(cc.p(mainLayer_Root.jifenbd.getContentSize().width/2,mainLayer_Root.jifenbd.getContentSize().height/2-2))
    mainLayer_Root.jifenbd.addChild(fenshu);


    var act_move_to4 = cc.moveBy(1,cc.p(0,-300));
    var actdelay4 = cc.DelayTime.create(1.33)
    var todo_act4 = cc.Sequence.create(actdelay4,act_move_to4);

    mainLayer_Root.jifenbd.runAction(todo_act4)

    //主角出现
    var hero = HeroHelper.CreatHero("nvzhu","stand");
    mainLayer_Root.hero=hero;
    hero.setPosition(cc.p(-40,mainLayer_Root.getContentSize().height/2+32));
    mainLayer_Root.addChild(hero,200000);
    var act_move_to3 = cc.moveBy(1,cc.p(150,0));
    var actdelay3 = cc.DelayTime.create(1.33)
    var todo_act3 = cc.Sequence.create(actdelay3,act_move_to3);
    mainLayer_Root.hero.runAction(todo_act3);

    //主角出现完怪物出现
    var actdelay5 = cc.DelayTime.create(2);
    var todo_act5 = cc.Sequence.create(actdelay5,cc.callFunc(UiHelper.Mshow, mainLayer_Root));
    mainLayer_Root.runAction(todo_act5);
};

var M_NUM = 1;
var M_TAG = 10000;
var M_TAG_MAX = 10005;

var MLIST = [];
//怪物出现
UiHelper.Mshow = function(){

    gamestate="start";

    //怪物出现
    //循环造出1个怪物并向左移动,1秒出现一个
    mainLayer_Root.schedule(UiHelper.CreatAndMove,0.8);

    //碰撞检测启动
    mainLayer_Root.schedule(UiHelper.CrashHero);

};
//创建并移动
UiHelper.CreatAndMove = function(){

    var random = parseInt(Math.random()*100);
    var upordown = 36;
    if(random<=50)
    {
        upordown=-36;
    }

    if(parseInt(M_NUM)==6)
    {
        M_NUM=1;
    }
    Mlog.c("创建》》》》》",M_NUM);

    var random2 = parseInt(Math.random()*66);

    var speed = 1.0;
    if(Score_NUM>=20)
    {
        random2 = -10;
        speed = 0.9;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.7);
    }
    else if(Score_NUM>=30)
    {
        random2 = -40;
        speed = 0.8;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.5);
    }
    else if(Score_NUM>=40)
    {
        random2 = -70;
        speed = 0.7;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.4);
    }
    else if(Score_NUM>=50)
    {
        speed = 0.6;
        random2 = -90;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.3);
    }
    else if(Score_NUM>=80)
    {
        random2 = -120;
        speed = 0.5;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.2);
    }
    else if(Score_NUM>=100)
    {
        random2 = -150;
        speed = 0.4;
        mainLayer_Root.unschedule(UiHelper.CreatAndMove);
        mainLayer_Root.schedule(UiHelper.CreatAndMove,0.2);
    }

    //创建
    var jifenbd = cc.Sprite.create(res.UI_ZA);
    jifenbd.x = mainLayer_Root.getContentSize().width+random2;
    jifenbd.y = mainLayer_Root.getContentSize().height/2+upordown;

    mainLayer_Root.addChild(jifenbd,1,parseInt(M_TAG)+parseInt(M_NUM));

    var act_move_to = cc.moveBy(speed,cc.p(-750,0));
    var todo_act = cc.Sequence.create(act_move_to,cc.callFunc(UiHelper.RemoveNode, jifenbd));
    jifenbd.runAction(todo_act);
    M_NUM++;
};

UiHelper.RemoveNode= function(){
    Score_NUM++;
    this.removeFromParent();
    if(mainLayer_Root.fenshu!=null)
    {
        mainLayer_Root.fenshu.setString(Score_NUM);
        mainLayer_Root.fenshu.setScale(1.4);
        var s_act = cc.ScaleTo.create(0.3,1);
        mainLayer_Root.fenshu.runAction(s_act);
    }
};

//碰撞检测
UiHelper.CrashHero= function(){
    var heroRect = UiHelper.getRect(mainLayer_Root.hero);

    for(var i = 10001; i<=10010; i++)
    {
        if(mainLayer_Root.getChildByTag(i)!=null)
        {
            var node = mainLayer_Root.getChildByTag(i);
            //Mlog.c("33");
            //进行检测
            var nowRect = UiHelper.getRect(node);
            var flag =  cc.rectIntersectsRect(heroRect,nowRect);
            if(flag==true)
            {
                //node.setVisible(false);
                Mlog.c("碰撞了");
                UiHelper.toDeath();
                gamestate="death";
                //执行屎忘操作
            }
        }
    }

};


UiHelper.toDeath = function(sp)
{
    OKTOUCH =false;
    //停止所有一切会动的东西
    mainLayer_Root.stopAllActions();
    for(var i = 10001; i<=10010; i++) {
        if (mainLayer_Root.getChildByTag(i) != null) {
            var node = mainLayer_Root.getChildByTag(i);
            node.stopAllActions();
        }
    }
    //注销定时器
    mainLayer_Root.unschedule(UiHelper.CreatAndMove);

    //碰撞检测启动
    mainLayer_Root.unschedule(UiHelper.CrashHero);

    //主角掉下去
    var act_move_to = cc.moveBy(1,cc.p(0,-500));
    //游戏结束
    var todo_act = cc.Sequence.create(act_move_to,cc.callFunc(UiHelper.gameOver, mainLayer_Root));
    mainLayer_Root.hero.runAction(todo_act);
}

//游戏结束
UiHelper.gameOver = function(sp)
{
    //清屏的干活
    mainLayer_Root.baseline.removeFromParent();
    mainLayer_Root.goto.removeFromParent();
    for(var i = 10001; i<=10010; i++) {
        if (mainLayer_Root.getChildByTag(i) != null) {
            mainLayer_Root.removeChildByTag(i);
        }
    }
    mainLayer_Root.hero.removeFromParent();
    mainLayer_Root.jifenbd.removeFromParent();

    GameOver.toRun(mainLayer_Root,Score_NUM);

    OKTOUCH=true;
    gamestate="0";
    Score_NUM=0;
}

UiHelper.getRect = function(sp)
{
    //获取精灵区域大小
    return cc.rect(sp.getPositionX()- sp.getContentSize().width  * sp.getAnchorPoint().x,sp.getPositionY()-sp.getContentSize().height* sp.getAnchorPoint().y,sp.getContentSize().width, sp.getContentSize().height);
}


